package com.feihong.yw189.activity.yonghuzhongxin.userinfo;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.blankj.utilcode.util.LogUtils;
import com.feihong.yw189.R;
import com.feihong.yw189.activity.base.BaseActivity;
import com.feihong.yw189.app.App;
import com.feihong.yw189.global.RequestTools;
import com.feihong.yw189.listener.http.RequestCallBack;
import com.feihong.yw189.message.LoginMessage;
import com.feihong.yw189.model.User.User;
import com.feihong.yw189.model.User.my.EditPhoneModel;
import com.feihong.yw189.utils.MessageEvent;
import com.feihong.yw189.utils.UserLoginInfo;

import org.greenrobot.eventbus.EventBus;
import org.xutils.common.Callback;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class EditPhoneNumActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.rl_goback)
    RelativeLayout rlGoback;
    @BindView(R.id.txt_pagetitle)
    TextView txtPagetitle;
    @BindView(R.id.iv_lin)
    ImageView ivLin;
    @BindView(R.id.iv_confirm)
    TextView ivConfirm;
    @BindView(R.id.top_back_layout)
    LinearLayout topBackLayout;
    @BindView(R.id.phoneName)
    TextView phoneName;
    @BindView(R.id.edit_phonenum)
    EditText editPhonenum;
    @BindView(R.id.tv_ok)
    Button tvOk;
    @BindView(R.id.loginPwd)
    TextView loginPwd;
    @BindView(R.id.loginPwd_txt)
    EditText loginPwdTxt;

    private Unbinder unbinder;
    private String smsNo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_phone_num);
        unbinder = ButterKnife.bind(this);

        Intent intent = getIntent();
        smsNo = intent.getStringExtra("sms_no");

        initView();
        bindData();
    }

    @Override
    public void setTheme() {

    }

    private void initView() {
        setPageTitle("gengssss");
        ivLin.setVisibility(View.GONE);
    }

    private void bindData() {

    }


    @Override
    public void onUserLogin(LoginMessage loginMessage) {

    }

    @OnClick({R.id.iv_back, R.id.rl_goback, R.id.tv_ok})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                setBackButtonFinish();
                break;
            case R.id.rl_goback:
                setBackButtonFinish();
                break;
            case R.id.tv_ok:
                httpMethod();
                break;
        }
    }

    private void httpMethod() {
        final String phone = "" + editPhonenum.getText();
        String pwd = "" + loginPwdTxt.getText().toString();
        if (phone.isEmpty()) {
            showErrorToast("请输入新手机号码!");
        }
        else if (phone.length() != 11) {
            showErrorToast("请输入正确的手号码!");
        }
        else if (pwd.isEmpty()) {
            showErrorToast("请输入登录密码!");
        } else {
            Callback.Cancelable callBack = App.getApp().getHttpTools().editPhoneNum(phone, pwd, smsNo, new RequestCallBack<String>() {
                @Override
                public void onSuccess(String result) {
                    LogUtils.i(TAG + "修改手机号返回结果" + result);
                    try {
                        EditPhoneModel model = JSON.parseObject(result, EditPhoneModel.class);
                        if (model != null) {
                            if (model.getStatus() == 1) {
                                UserLoginInfo.getUserLoginInfoInstance().setUserMobile(phone);
                                //通知
                                EventBus.getDefault().post(new MessageEvent(MessageEvent.EDIT_PHONE));
                                finish();
                                showToast(model.getInfo());
                            } else {
                                if (model.getInfo().contains("验证码错误")) {
                                    finish();
                                    Intent intent = new Intent(getApplication(), UserInfoEditActivity.class);
                                    intent.putExtra("type", 1);
                                    startActivity(intent);
                                    showToast(model.getInfo() + "请重新输入!");
                                } else {
                                    showErrorToast(model.getInfo());
                                }
                            }
                        } else {
                            showErrorToast("解析出错,请重试");
                        }
                    } catch (Exception ex) {
                    }
                }

                @Override
                public void onError(Throwable ex, boolean isOnCallback) {
                    showToast(getResources().getString(R.string.connect_sever_error_toast) );
                }

                @Override
                public void onCancelled(Callback.CancelledException cex) {
                }

                @Override
                public void onFinished() {
                }
            });
            RequestTools.addNewRequest(EditPhoneNumActivity.class.getName(), callBack);
        }
    }

    @Override
    public void setBackButtonFinish() {
        if (ivBack != null) {
            ivBack.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                }
            });
        }
    }

    @Override
    protected void onDestroy() {
        // 清空请求
        RequestTools.removeRequest(EditPhoneNumActivity.class.getName());
        super.onDestroy();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }
}
